package my.diplom.util;

import my.diplom.entity.Host;
import my.diplom.entity.Resource;
import my.diplom.entity.Project;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Arrays;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 18.11.2009
 * Time: 0:22:33
 * To change this template use File | Settings | File Templates.
 */
public class ReportUtil {

    public static final String n = System.getProperty("line.separator");
    public static final String tab = "    ";


    public static String generateReportByHost(Host host) {
        Host [] array = {host};
        HostUtil.setUsage(Arrays.asList(array));

        String report = n + "================================================" + n;
        report += "Host name = " + host.getHostName() + n;
        report += "Ip address = " + host.getIpAddress() + n;
        report += "CPU = " + host.getCpu() + "Usage = " + host.getCpuUsage() + n;
        report += "RAM = " + host.getRam() + "Usage = " + host.getRamUsage() + n;
        report += "HDD = " + host.getHdd() + "Usage = " + host.getHddUsage() + n;
        report += "Description = " + host.getDescription() + n;
        report += "Current status = " + host.getStatus().getAlias() + n;
        if (!(host.getResources().size() > 0)) return report;
        report += "Resources Information" + n;
        for (Resource r: host.getResources()){
            report += tab;
            report += "Rresource = " + r.getHostName() + n + tab;
            report += "CPU = " + r.getCpu() + n + tab;
            report += "RAM = " + r.getRam() + n + tab;
            report += "HDD = " + r.getHdd() + n + tab;
            report += "Start = " + r.getStartDate() + n + tab;
            report += "Finish = " + r.getFinishDate() + n + tab;
            report += "Description = " + r.getDescription() + n + tab;
            report += "Status = " + r.getStatus().getAlias() + n + tab;
            if (!(r.getProjects().size() > 0)) continue;
            report += "Involved projects" + n;
            for (Project p: r.getProjects()){
                report += tab + tab;
                report += "Project = " + p.getProjectName() + n +tab + tab;
            }
        }

        try {
            File file = new File("C:\\reports\\report.txt");
            FileWriter writer = new FileWriter(file, true);
            writer.write(report);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(report);
        return report;
    }

    public static void generateReportByResource(Resource resource) {


    }


}
